package com.gz.tank;

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable{
    Vector<Shot> shots=new Vector<>();
    Vector<EnemyTank>enemies=new Vector<>();

    public void setEnemies(Vector<EnemyTank> enemies) {
        this.enemies = enemies;
    }

    public EnemyTank(int x, int y) {
        super(x, y);
    }


    //判断敌人坦克是否碰撞
    public boolean isenemytouch() {//有待优化
        switch (this.getDirect()){
            case 0://坦克朝上
                for(int i=0;i<enemies.size();i++){
                    EnemyTank enemyTank=enemies.get(i);
                    if(enemyTank!=this){
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            //左上角
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;

                            //右上角
                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;
                        }
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;

                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;
                        }
                    }
                }
                break;
            case 1://坦克朝右
                for(int i=0;i<enemies.size();i++){
                    EnemyTank enemyTank=enemies.get(i);
                    if(enemyTank!=this){
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            //右上角
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;

                            //右下角
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+40
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+60)
                                return true;
                        }
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;

                            //右下角
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+40
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+60)
                                return true;
                        }
                    }
                }
                break;
            case 2://坦克朝下
                for(int i=0;i<enemies.size();i++){
                    EnemyTank enemyTank=enemies.get(i);
                    if(enemyTank!=this){
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            //左下角
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+60)
                                return true;

                            //右下角
                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+40
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+60)
                                return true;
                        }
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+60
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+40)
                                return true;

                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+60
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+40)
                                return true;
                        }
                    }
                }
                break;
            case 3://坦克朝左
                for(int i=0;i<enemies.size();i++){
                    EnemyTank enemyTank=enemies.get(i);
                    if(enemyTank!=this){
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            //左上角
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;

                            //左下角
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+60)
                                return true;
                        }
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;

                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+60)
                                return true;
                        }
                    }
                }
                break;
        }
        return false;
    }
    @Override
    public void run() {//敌人坦克移动
        while(true){
            //敌人坦克发送多颗子弹，即子弹消亡，子弹数组为空且坦克存在时发送
            if(shots.size()==0 && islive){
                Shot shot=null;
                switch (getDirect()){//根据敌人坦克朝向给敌人坦克分配子弹
                    case 0:
                        shot=new Shot(getX()+20,getY(),0);
                        break;
                    case 1:
                        shot=new Shot(getX()+60,getY()+20,1);
                        break;
                    case 2:
                        shot=new Shot(getX()+20,getY()+60,2);
                        break;
                    case 3:
                        shot=new Shot(getX(),getY()+20,3);
                        break;
                }
                shots.add(shot);
                new Thread(shot).start();//这里部分与我的坦克hero一样，因为我的按J发送，要求不同，故不写在父类

            }
            switch (getDirect()){
                case 0://向上
                    for (int i = 0; i <30 ; i++) {//每换一个方向走30步moveup();
                        if(!isenemytouch())
                            moveup();
                       try {
                            Thread.sleep(50);
                       } catch (InterruptedException e) {
                            e.printStackTrace();
                       }
                    }
                    break;
                case 1://向右
                    for (int i = 0; i <30 ; i++) {
                        if(!isenemytouch())
                            moveright();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                        break;

                case 2://向下
                    for (int i = 0; i <30 ; i++) {
                        if(!isenemytouch())
                            movedown();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                        break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if(!isenemytouch())
                            moveleft();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                }
            setDirect((int)(Math.random()*4));
            if(islive==false){
                break;
            }
        }
    }
}
